from datetime import datetime, timedelta
from utils.dateutil import date_now

class SimpleCache(object):
    def __init__(self, **expire):
        self._timer = date_now()
        self._expire = timedelta(**expire)
        self._cache = None

    def is_expired(self):
        return date_now() - self._timer > self._expire

    def reset_timer(self):
        self._timer = date_now()

    def is_need_update_cache(self):
        return self.cache is None or self.is_expired()

    @property
    def cache(self):
        return self._cache

    @cache.setter
    def cache(self, value):
        self._cache = value
        self.reset_timer()
